package com.cuiyun97.aspect;

import com.cuiyun97.constant.ContextConstant;
import com.cuiyun97.enumeration.OperationType;
import com.cuiyun97.annotation.AutoFillField;
import com.cuiyun97.constant.AutoFillConstant;
import com.cuiyun97.context.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * 自定义切面, 实现自动填充字段
 *
 */
@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    /**
     * 切入点
     */

    @Pointcut("execution(* com.cuiyun97.mapper.*.*(..)) " +
            "&& @annotation(com.cuiyun97.annotation.AutoFillField)")
    public void autoFillPointcut() {}

    /**
     * 前置通知, 在通知中进行字段填充
     *
     */
    @Before("autoFillPointcut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("开始字段自动填充...");

        // 获取到当前被拦截的方法上的数据库操作类型
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        AutoFillField autoFillField = signature.getMethod().getAnnotation(AutoFillField.class);
        OperationType operationType = autoFillField.value();

        // 获取到当前被拦截的方法参数 -- 实例对象
        // 约定实体对象放在第1个参数
        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }

        // 准备赋值的数据
        Object entity = args[0];
        LocalDateTime now = LocalDateTime.now();
        Map<String, Integer> map = BaseContext.get();
        Integer adminId = map.get(ContextConstant.ADMIN_FLAG);

        // 根据当前不同的操作类型, 为对应的属性通过反射来赋值
        try {
            Class<?> c = entity.getClass();
            if (operationType == OperationType.INSERT) {
                Method setCreateTime = c.getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method setCreateUser = c.getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Integer.class);
                Method setUpdateTime = c.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = c.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Integer.class);
                setCreateTime.invoke(entity, now);
                setCreateUser.invoke(entity, adminId);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, adminId);
            } else if (operationType == OperationType.UPDATE) {
                Method setUpdateTime = c.getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method setUpdateUser = c.getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Integer.class);
                setUpdateTime.invoke(entity, now);
                setUpdateUser.invoke(entity, adminId);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
